<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="customerId">
        <el-input
          v-model="queryParams.customerId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单ID" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申诉时间" prop="complaintTime">
        <el-date-picker clearable
          v-model="queryParams.complaintTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择申诉时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申诉状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="待处理" value="0"></el-option>
          <el-option label="已处理" value="1"></el-option>
          <el-option label="已驳回" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:complaint:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:complaint:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:complaint:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="complaintList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申诉ID" align="center" prop="complaintId" />
      <el-table-column label="用户ID" align="center" prop="customerId" />
      <el-table-column label="用户姓名" align="center" prop="name" />
      <el-table-column label="申诉理由" align="center" prop="complaintReason" />
      <el-table-column label="订单ID" align="center" prop="orderId" />
      <el-table-column label="处理方案" align="center" prop="solution" />
      <el-table-column label="申诉时间" align="center" prop="complaintTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.complaintTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申诉状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:complaint:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:complaint:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户申诉对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="用户姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入用户姓名" />
        </el-form-item>
        <el-form-item label="申诉理由" prop="complaintReason">
          <el-input v-model="form.complaintReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="处理方案" prop="solution">
          <el-input v-model="form.solution" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="申诉时间" prop="complaintTime">
          <el-date-picker clearable
            v-model="form.complaintTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申诉时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申诉状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择申诉状态">
            <el-option label="待处理" value="0"></el-option>
            <el-option label="已处理" value="1"></el-option>
            <el-option label="已驳回" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listComplaint, getComplaint, delComplaint, addComplaint, updateComplaint } from "@/api/system/complaint"

export default {
  name: "Complaint",
  data() {
    const isNumber = (rule, value, callback) => {
      if (value === null || value === undefined || value === '') {
        callback(new Error('请输入内容'))
      } else if (!Number.isInteger(Number(value))) {
        callback(new Error('请输入有效的数字'))
      } else {
        callback()
      }
    }
    
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      complaintList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: null,
        name: null,
        complaintReason: null,
        orderId: null,
        solution: null,
        complaintTime: null,
        status: null
      },
      form: {
        complaintId: null,
        customerId: null,
        name: null,
        complaintReason: null,
        orderId: null,
        solution: null,
        complaintTime: null,
        status: "0"
      },
      rules: {
        customerId: [
          { required: true, validator: isNumber, trigger: "blur" }
        ],
        name: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" }
        ],
        complaintReason: [
          { required: true, message: "申诉理由不能为空", trigger: "blur" }
        ],
        orderId: [
          { required: true, validator: isNumber, trigger: "blur" }
        ],
        status: [
          { required: true, message: "请选择申诉状态", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      const params = { ...this.queryParams, t: new Date().getTime() }
      listComplaint(params).then(response => {
        this.complaintList = response.rows.map(item => ({
          ...item,
          status: String(item.status)
        }))
        this.total = response.total
        this.loading = false
      })
    },
    /** 状态文本转换 */
    getStatusText(status) {
      const statusMap = {
        '0': '待处理',
        '1': '已处理',
        '2': '已驳回'
      }
      return statusMap[status] || '未知状态'
    },
    /** 状态标签类型转换 */
    getStatusType(status) {
      const typeMap = {
        '0': 'warning',
        '1': 'success',
        '2': 'danger'
      }
      return typeMap[status] || 'info'
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        complaintId: null,
        customerId: null,
        name: null,
        complaintReason: null,
        orderId: null,
        solution: null,
        complaintTime: null,
        status: "0"
      }
      this.resetForm("form")
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.complaintId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加用户申诉"
    },
    handleUpdate(row) {
      this.reset()
      const complaintId = row ? row.complaintId : this.ids[0]
      getComplaint(complaintId).then(response => {
        this.form = {
          ...response.data,
          status: String(response.data.status),
          name: response.data.name || ''
        }
        this.open = true
        this.title = "修改用户申诉"
      })
    },
    /** 提交表单 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.complaintId != null) {
            updateComplaint(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addComplaint(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    handleDelete(row) {
      const complaintIds = row ? row.complaintId : this.ids
      this.$modal.confirm(`是否确认删除用户申诉编号为"${complaintIds}"的数据项？`).then(() => {
        return delComplaint(complaintIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    }
  }
}
</script>
